package com.eascs.spider.ant.schdule.dispatcher;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.spider.ant.common.proj.Task;
import com.eascs.spider.ant.common.util.HttpUtil;
import com.eascs.spider.ant.common.util.PropertiesUtil;
import com.eascs.spider.ant.schdule.bloomfilter.BloomFilter;
import com.eascs.spider.ant.schdule.register.RegisterCentre;
import com.google.gson.Gson;

/**
 * 任务分发
 * @author hzp
 *
 */
@Service
public class Dispatcher {
	private static Logger logger = LoggerFactory.getLogger(Dispatcher.class);
	
	private Queue<Task> queue;//任务优先级队列
	private static String workerRecievePath = PropertiesUtil.getConfig("worker.receive.path");
	
	@Autowired
	BloomFilter bf;

	@Autowired
	RegisterCentre registerCentre;
	
	Dispatcher() {
		 queue = new ConcurrentLinkedQueue<Task>();
		 CloseableHttpClient client = HttpUtil.initHttpClient();
		 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
				@Override
				public void run() {
					if(!queue.isEmpty()){
						String workerPath = registerCentre.getRandomWorkerAddress();
						Task task = queue.poll();
						logger.info("task:{}" ,new Gson().toJson(task));
						HttpUtil.executePost(client, "http://" + workerPath + workerRecievePath, task);
					}
				}
			 } ,100 ,50 ,TimeUnit.MILLISECONDS);
	}
	
	public synchronized void addTask(Task task){
		//放行新农商网供应信息,价格行情
		if(whiteList(task)){
			queue.add(task);
			return;
		}
			
		//加入布隆去重的有新农商网的店铺url，新农商网的农业资讯单篇文章url
		//新浪微博，新浪新闻，凤凰资讯
		if(!bf.contains(task.getUrl())){
			queue.add(task);
			bf.add(task.getUrl());
			return;
		}
		logger.info("未添加任务队列,url:{}" ,task.getUrl());
	}
	
	//加入任务队列前先过白名单
	private boolean whiteList(Task task){
		//新农商网价格行情(翻页post请求)，供应信息(翻页get请求),中关村手机,汽车之家
		String[] whiteList = {"http://nc.mofcom.gov.cn/channel/jghq2017/price_list.shtml"
							  ,"http://nc.mofcom.gov.cn/channel/gxdj/gyxx"
							  ,"http://detail.zol.com.cn/cell_phone_index/subcate57_(\\d+)_list_1_0_1_1_0_1.html"
							  ,"https://dealer.autohome.com.cn.*?"
							  ,"http://news.cnhnb.com/hangqing.*?"
							  ,"http://news.cnhnb.com/zixun.*?"
							  ,"https://dealer.autohome.com.cn.*?"
							  ,"http://www.cnhnb.com/caigou/.*?"
							  ,"http://www.cnhnb.com/purchase.*?"
							  ,"http://www.cnhnb.com/supply/"
							  ,"http://www.cnhnb.com/p.*?"
							  ,"http://www.cnhnb.com/gongying.*?"};
		for(String temp : whiteList){
			if(task.getUrl().matches(temp))
				return true;
		}
		return false;
	}
	
	/**
	 * 添加首页请求任务,不需去重
	 * @param task
	 */
	public void addHomepageTask(Task task){
		queue.add(task);
	}
	
	public int taskSize(){
		return queue.size();
	}
	
}